﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using Gman.WebApp.GestionServiceRef;
using Gman.WebApp.OperacionServiceRef;
using Gman.WebApp.PresupuestoServiceRef;
using Gman.WebApp.SeguimientoServiceRef;
using Gman.WebApp.GeneralServiceRef;

namespace Gman.WebApp.ServiceControllers
{
    public class ServiceControllerBase
    {

        #region Configuracion WCF

        /// <summary>
        /// Client tag provided by the service provider and stored locally. 
        /// This value must be provided with every service call.
        /// </summary>
        protected static string ClientTag { get; private set; }

        /// <summary>
        /// Static constructor
        /// </summary>
        static ServiceControllerBase()
        {
            // Retrieve ClientTag from web config file
            ClientTag = ConfigurationManager.AppSettings.Get("ClientTag");
        }

        // The access token that was returned from the service.
        // This value must be provided in every call for the duration of the session.
        //private string _accessToken;

        /// <summary>
        /// Gets or sets access token. If no token exists a new one is retrieved from service.
        /// </summary>
        protected string AccessToken
        {
            get
            {
                if (HttpContext.Current.Session["AccessToken"] == null)
                {
                    // Request a unique accesstoken from the webservice. This token is
                    // that is valid for the duration of the session.                    
                }
                return (string)HttpContext.Current.Session["AccessToken"];
            }
        }

        /// <summary>
        /// Gets a new random GUID request id.
        /// </summary>
        protected string NewRequestId
        {
            get { return Guid.NewGuid().ToString(); }
        }


        #endregion

        protected GestionServiceClient GestionServiceClient
        {
            get
            {
                if (HttpContext.Current.Session["GestionServiceClient"] == null)
                    HttpContext.Current.Session["GestionServiceClient"] = new GestionServiceClient();

                return HttpContext.Current.Session["GestionServiceClient"] as GestionServiceClient;
            }
        }

        protected OperacionServiceClient OperacionServiceClient
        {
            get
            {
                if (HttpContext.Current.Session["OperacionServiceClient"] == null)
                    HttpContext.Current.Session["OperacionServiceClient"] = new OperacionServiceClient();

                return HttpContext.Current.Session["OperacionServiceClient"] as OperacionServiceClient;
            }
        }

        protected PresupuestoServiceClient PresupuestoServiceClient
        {
            get
            {
                if (HttpContext.Current.Session["PresupuestoServiceClient"] == null)
                    HttpContext.Current.Session["PresupuestoServiceClient"] = new PresupuestoServiceClient();

                return HttpContext.Current.Session["PresupuestoServiceClient"] as PresupuestoServiceClient;
            }
        }

        protected SeguimientoServiceClient SeguimientoServiceClient
        {
            get
            {
                if (HttpContext.Current.Session["SeguimientoServiceClient"] == null)
                    HttpContext.Current.Session["SeguimientoServiceClient"] = new SeguimientoServiceClient();

                return HttpContext.Current.Session["SeguimientoServiceClient"] as SeguimientoServiceClient;
            }
        }

        protected GeneralServiceClient GeneralServiceClient
        {
            get
            {
                if (HttpContext.Current.Session["GeneralServiceClient"] == null)
                    HttpContext.Current.Session["GeneralServiceClient"] = new GeneralServiceClient();

                return HttpContext.Current.Session["GeneralServiceClient"] as GeneralServiceClient;
            }
        }

    }
}